<template>
  <div class="" style="margin-left: 10%">
    <el-form
      ref="passwordForm"
      :rules="passwordRules"
      :model="passwordForm"
      label-width="80px"
    >
      <el-form-item label="旧密码" prop="oldPassword">
        <el-col :span="8">
          <el-input v-model="passwordForm.oldPassword" clearable></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-col :span="8">
          <el-input v-model="passwordForm.newPassword" clearable></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="密码" prop="confirmPassword">
        <el-col :span="8">
          <el-input v-model="passwordForm.confirmPassword" clearable></el-input>
        </el-col>
      </el-form-item>
    </el-form>
    <span style="margin-left: 33%">
      <el-button type="primary" @click="handleChangePassword">确 定</el-button>
    </span>
  </div>
</template>

<script>
export default {
  name: 'Password',
  data() {
    let checkOldPassword = (rule, value, callback) => {
      if (value === this.user.password) {
        callback()
      } else {
        callback(new Error('旧密码错误！'))
      }
    }
    let checkConfirmPassword = (rule, value, callback) => {
      if (this.passwordForm.confirmPassword === this.passwordForm.newPassword) {
        callback()
      } else {
        callback(new Error('两次密码不一致！'))
      }
    }
    return {
      user: JSON.parse(localStorage.getItem('curUser')) || {},
      passwordDialogVisible: false,
      passwordForm: {
        id: (JSON.parse(localStorage.getItem('curUser')) || {}).id,
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      passwordRules: {
        oldPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' },
          { validator: checkOldPassword, trigger: 'submit' }
        ],
        newPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' },
          { validator: checkConfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    passwordChange() {
      this.passwordDialogVisible = true

      this.$nextTick(() => {
        this.passwordForm.id = this.user.id
      })
    },
    handleChangePassword() {
      this.$refs.passwordForm.validate(valid => {
        if (valid) {
          this.$http
            .post('/user/update', {
              id: this.user.id,
              password: this.passwordForm.newPassword
            })
            .then(res => {
              console.log(res)
              if (res.code == 200) {
                this.$message.success('修改密码成功！请重新登录！')
                this.passwordDialogVisible = false
                this.$router.push('/login')
                localStorage.removeItem('curUser')

                this.$refs.passwordForm.resetFields()
              } else {
                this.$message.error('修改密码失败！')
              }
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped></style>
